
#include <gtk-3.0/gtk/gtk.h>
#include <string.h>
#include <stdlib.h>
#include "track_data_widget.h"
#include "track_box_widget.h"
#include "tux_gsettings.h"
#include "audio_device_dialog_widget.h"
#include "alsa_detect.h"
#include "pulseaudio_detect.h"
#include "jack_detect.h"
#include "main_window_widget.h"
#include "recording_buffer.h"

typedef struct track_data_widget_private TrackDataWidgetPrivate;



struct _track_data {
    gdouble timestamp;
    guint32 recorded_buffer_key;
    gdouble from_time;
    gdouble to_time;
};

struct _track_data_widget_class
{
    GtkBoxClass parent_class;
};

struct track_data_widget_private 
{
    GtkWidget *track_data_fixed;
};

struct _track_data_widget 
{
    GtkBox parent;
    TrackDataWidgetPrivate *priv;
    GArray *track_data_array;
    guint32 current_track_recording_data_key;

    
};


G_DEFINE_TYPE_WITH_PRIVATE(TrackDataWidget,track_data_widget,GTK_TYPE_BOX);

enum {
    PROP_0,
};


GArray *get_current_track_data_small_values(TrackDataWidget *widget) {
    g_assert(widget->current_track_recording_data_key != 0);
    return recording_buffer_get_buffer_small_data(widget->current_track_recording_data_key);
}

TrackData *new_current_track_data(TrackDataWidget *widget) {
    widget->current_track_recording_data_key = recording_buffer_new_buffer();
    TrackData *track_data = g_new0(TrackData, 1);
    track_data->recorded_buffer_key=widget->current_track_recording_data_key;
    g_array_append_val(widget->track_data_array,track_data);
    track_data->timestamp=(marker_location == -1 ? 0 : marker_location);
    return track_data;
}    


void close_current_track_data(TrackDataWidget *widget) {
    widget->current_track_recording_data_key=0;
}

static void track_data_widget_draw_recordings(TrackDataWidget *widget, cairo_t *cr, GtkWidget *g_widget) {
    GArray *track_data_array = widget->track_data_array;
    guint width = gtk_widget_get_allocated_width (g_widget);
    guint height = gtk_widget_get_allocated_height (g_widget);
    guint i, j;
    double center = height/2;        
    double x_position;
    if (track_data_array->len>0) {
        for (i=0;i<track_data_array->len;i++) {
            TrackData *data = g_array_index(track_data_array,TrackData *,i);
            GArray *data_small = recording_buffer_get_buffer_small_data(data->recorded_buffer_key);
            x_position = data->timestamp;
            cairo_move_to(cr, x_position++,center);        
            if (data_small->len > 0) {
                for (j=0;j<data_small->len;j++) {
                    gint8 data_sound = g_array_index(data_small, gint8, j);
                    cairo_line_to(cr, x_position++, data_sound);
                }    
            }
        }
    }
    
}

static gboolean track_data_widget_draw(GtkWidget *widget, cairo_t *cr, gpointer data) {
    guint width = gtk_widget_get_allocated_width (widget);
    guint height = gtk_widget_get_allocated_height (widget);
    guint i=0;
    double center = height/2;
    cairo_set_source_rgb(cr, 0, 0, 0);
    cairo_set_line_width(cr, 0.5);
    if (marker_location>=0) {
        cairo_move_to(cr,marker_location,0);
        cairo_line_to(cr,marker_location,height);
    }
    if (track_location>=0) {
        cairo_move_to(cr,track_location,0);
        cairo_line_to(cr,track_location,height);          
    }
    track_data_widget_draw_recordings((TrackDataWidget *)data,cr,widget);
    cairo_stroke(cr);
}

static track_data_widget_pressed(GtkWidget *gladewidget,
                                                        GdkEvent    *arg1,
                                                        gpointer     user_data) {
    g_print("pressed");
}

GtkFixed *get_track_data_fixed(TrackDataWidget *widget) {
    return GTK_FIXED(widget->priv->track_data_fixed);
}

static void track_data_widget_init(TrackDataWidget *widget) {
    widget->priv = G_TYPE_INSTANCE_GET_PRIVATE(widget, TRACK_DATA_WIDGET_TYPE, TrackDataWidgetPrivate);
    gtk_widget_init_template(GTK_WIDGET(widget));   
   
}


TrackDataWidget *track_data_widget_new(void) {
    TrackDataWidget *widget= g_object_new(TRACK_DATA_WIDGET_TYPE,NULL);
    widget->track_data_array = g_array_new(TRUE, TRUE, sizeof(TrackData *));
    widget->current_track_recording_data_key = 0;
    return widget;
}


static void
track_data_widget_set_property (GObject         *object,
                         guint            prop_id,
                         const GValue    *value,
                         GParamSpec      *pspec)
{
  TrackDataWidget *widget = TRACK_DATA_WIDGET (object);

  switch (prop_id)
    {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
track_data_widget_get_property (GObject         *object,
                        guint            prop_id,
                        GValue          *value,
                        GParamSpec      *pspec)
{
  TrackDataWidget *widget = TRACK_DATA_WIDGET (object);

  switch (prop_id)
    {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void track_data_widget_class_init(TrackDataWidgetClass *class) {
    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(class);
    GObjectClass *gobject_class = G_OBJECT_CLASS(class);
    
    gobject_class->set_property=track_data_widget_set_property;
    gobject_class->get_property=track_data_widget_get_property;
    
    gtk_widget_class_set_template_from_resource (widget_class,
                                               "/com/tuxin/tuxmusicstudio/track_data.glade");
    gtk_widget_class_bind_template_child_private(widget_class, TrackDataWidget, track_data_fixed);
    gtk_widget_class_bind_template_callback(widget_class, track_data_widget_draw);
    gtk_widget_class_bind_template_callback(widget_class, track_data_widget_pressed);
}

